package com.mygenostics.backing;

import static com.mygenostics.constants.SessionConstants.*;
import static com.mygenostics.constants.ApplicationConstants.*;

import java.io.File;
import java.sql.Timestamp;

import javax.faces.application.FacesMessage;
import javax.faces.context.FacesContext;

import org.richfaces.event.UploadEvent;
import org.richfaces.model.UploadItem;

import com.mygenostics.bo.SampleFile;
import com.mygenostics.bo.Service1;
import com.mygenostics.bo.ShoppingCart;
import com.mygenostics.misc.Util;
import com.mygenostics.misc.WebUtil;
import com.mygenostics.persistence.UserBean;
import com.mygenostics.persistence.UserFile;

public class Service1Backing {
	
	private String fileType = "n";
	private SampleFile sampleFile;
	private boolean autoUpload = false;
	private boolean useFlash = false;
	private long itemId = 0l;
	private int libraryNum;
	private int seqNum;
	
	public String addToCart() {
		FacesContext context = FacesContext.getCurrentInstance();
		
		UserBean currentUser = (UserBean)WebUtil.getSessionMap().get(CURRENT_USER);
		
		ShoppingCart shoppingCart = (ShoppingCart)WebUtil.getSessionMap().get(SHOPPING_CART);
		
		if (shoppingCart == null) {
			shoppingCart = new ShoppingCart();
		}
		
		
		
		if (itemId > 0) {
		
		}
		else {
			
			Service1 service = new Service1();
			if (fileType.equalsIgnoreCase("n")) {
				if (sampleFile == null) {
					FacesMessage fm = new FacesMessage(FacesMessage.SEVERITY_ERROR, "Sample File Missing", "Sample file is required for this service.");
					context.addMessage(null, fm);
		            return "validation_failed";
				}
				
				UserFile uFile = new UserFile();
				uFile.setUser(currentUser);
				uFile.setOrigName(sampleFile.getName());
				String fileExtension = Util.getFileExtension(sampleFile.getName());
				String nfileName = Util.generateRandomFileName();
				uFile.setFileName(nfileName);
				String filePath = Util.CONFIG.getProperty(TEMP_FOLDER);
				filePath += File.separator;
				filePath += nfileName;
				filePath += ".";
				filePath += fileExtension;
				sampleFile.getFile().renameTo(new File(filePath));
				uFile.setSubFolder(currentUser.getUserName());
				uFile.setUploadDate(new Timestamp(System.currentTimeMillis()));
				service.setUserFile(uFile);
			}
			else {
				
			}
			
			service.updateLibraryConstruction(libraryNum);
			service.updateSolexaSequence(seqNum);
			shoppingCart.updateItem(itemId, service);
			WebUtil.getSessionMap().put(SHOPPING_CART, shoppingCart);
		}
		return "to_user_home";
	}
	
	public String orderNew() {
		return "";
	}
	
	public void fileUploadlistener(UploadEvent event) throws Exception{
        UploadItem item = event.getUploadItem();
        File file = item.getFile();
        if (file != null) {
        	SampleFile sample = new SampleFile();
        	sample.setFile(file);
        	sample.setName(item.getFileName());
        	sample.setLength(file.length());
        	sampleFile = sample;
        }
        
    }  
	
	public String getFileType() {
		return fileType;
	}

	public void setFileType(String fileType) {
		this.fileType = fileType;
	}

	public SampleFile getSampleFile() {
		return sampleFile;
	}

	public void setSampleFile(SampleFile sampleFile) {
		this.sampleFile = sampleFile;
	}

	public boolean isAutoUpload() {
		return autoUpload;
	}

	public void setAutoUpload(boolean autoUpload) {
		this.autoUpload = autoUpload;
	}

	public boolean isUseFlash() {
		return useFlash;
	}

	public void setUseFlash(boolean useFlash) {
		this.useFlash = useFlash;
	}

	public long getItemId() {
		return itemId;
	}

	public void setItemId(long itemId) {
		this.itemId = itemId;
	}

	public int getLibraryNum() {
		return libraryNum;
	}

	public void setLibraryNum(int libraryNum) {
		this.libraryNum = libraryNum;
	}

	public int getSeqNum() {
		return seqNum;
	}

	public void setSeqNum(int seqNum) {
		this.seqNum = seqNum;
	}

}
